package com.evan.uyclub.Bizhi;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.evan.uyclub.Other.LoadDialog;
import com.evan.uyclub.Other.SetClass;
import com.evan.uyclub.Other.Utlis;
import com.evan.uyclub.Other.VPNActivity;
import com.evan.uyclub.R;
import com.github.clans.fab.FloatingActionButton;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.tapadoo.alerter.Alerter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class ActivityBizhi extends AppCompatActivity implements View.OnClickListener {

    TextView item_style_text, title;
    ImageView item_style, back;
    RecyclerView recyclerView;
    LinearLayout bizhi_ll;
    ArrayList<BizhiData> dataArrayList = new ArrayList<>();
    BizhiAdapter adapter;
    FloatingActionButton fl_1, fl_2, fl_3, fl_4, fl_5, fl_6, fl_7, fl_8, fl_9, fl_10;
    ObjectAnimator objectAnimator, objectAnimator1;
    private VPNActivity vpnActivity;
    private Retrofit retrofit;
    private LoadDialog loadDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bizhi);
        //退出
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //申请dialong加载弹窗
        loadDialog = new LoadDialog(this);
        //vpn检测
        vpnActivity = new VPNActivity(this);
        //adapter加载
        SetClass.status(this);
        recyclerView = findViewById(R.id.recyclerview);
        adapter = new BizhiAdapter(ActivityBizhi.this, dataArrayList);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setAdapter(adapter);
        Getdata();
        //排列方式
        title = findViewById(R.id.title);
        bizhi_ll = findViewById(R.id.bizhi_ll);
        item_style = findViewById(R.id.item_style);
        item_style_text = findViewById(R.id.iitem_style_text);
        item_style.setOnClickListener(this::onClicks);
        //这是首次点击，可直接调用
//        item_style.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (item_style_text.getText().equals("0")){
//                    adapter=new BizhiAdapter(ActivityBizhi.this,dataArrayList);
//                    recyclerView.setLayoutManager(new GridLayoutManager(ActivityBizhi.this,1));
//                    recyclerView.setAdapter(adapter);
//                    Getdata();
//                    item_style_text.setText("1");
//                    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(bizhi_ll,"Alpha",0f,1f);
//                    objectAnimator.setDuration(2500);
//                    objectAnimator.start();
//                }
//                else{
//                    adapter=new BizhiAdapter(ActivityBizhi.this,dataArrayList);
//                    recyclerView.setLayoutManager(new GridLayoutManager(ActivityBizhi.this,2));
//                    recyclerView.setAdapter(adapter);
//                    Getdata();
//                    item_style_text.setText("0");
//                    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(bizhi_ll,"Alpha",0f,1f);
//                    objectAnimator.setDuration(2500);
//                    objectAnimator.start();
//                }
//            }
//        });
        //获取FloatingActionButton
        //获取FloatingActionButton
        fl_1 = findViewById(R.id.fl_1);
        fl_1.setOnClickListener(this);
        fl_2 = findViewById(R.id.fl_2);
        fl_2.setOnClickListener(this);
        fl_3 = findViewById(R.id.fl_3);
        fl_3.setOnClickListener(this);
        fl_4 = findViewById(R.id.fl_4);
        fl_4.setOnClickListener(this);
        fl_5 = findViewById(R.id.fl_5);
        fl_5.setOnClickListener(this);
        fl_6 = findViewById(R.id.fl_6);
        fl_6.setOnClickListener(this);
        fl_7 = findViewById(R.id.fl_7);
        fl_7.setOnClickListener(this);
        fl_8 = findViewById(R.id.fl_8);
        fl_8.setOnClickListener(this);
        fl_9 = findViewById(R.id.fl_9);
        fl_9.setOnClickListener(this);
        fl_10 = findViewById(R.id.fl_10);
        fl_10.setOnClickListener(this);

        //弹回
        SmartRefreshLayout app_smarts = (SmartRefreshLayout) findViewById(R.id.app_smarts);
        app_smarts.setEnableLoadMore(true);
        app_smarts.setEnablePureScrollMode(true);
    }

    //关闭弹窗
    public void Lao() {
        loadDialog.dismiss();
    }

    //每日精选
    public void Getdata() {
        if (!Utlis.isVPNConnected(this)) {
            loadDialog.show(false);
            dataArrayList.clear();
            retrofit = new Retrofit.Builder()
                    .baseUrl("http://wallpaper.apc.360.cn/")
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
            BizhiGet1 bizhiGet = retrofit.create(BizhiGet1.class);
            Call<String> stringCall = bizhiGet.STRING_CALL();
            stringCall.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        //加载成功的话关闭弹出
                        Lao();
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            String data = jsonObject.getString("data");
                            JSONArray jsonArray = new JSONArray(data);
                            StartAdapter(jsonArray);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    //无需处理
                }
            });
            //延迟关闭
            // new Handler().postDelayed(this::Lao,3000);
        }
    }

    //适配器
    public void StartAdapter(JSONArray jsonArray) {
        dataArrayList.clear();
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                BizhiData bizhiData = new BizhiData();
                bizhiData.setImage(jsonObject.getString("url"));
                dataArrayList.add(bizhiData);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            adapter = new BizhiAdapter(ActivityBizhi.this, dataArrayList);
            recyclerView.setAdapter(adapter);
        }
    }

    //每日精选
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_1:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet1 bizhiGet1 = retrofit.create(BizhiGet1.class);
                    Call<String> stringCall1 = bizhiGet1.STRING_CALL();
                    stringCall1.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    // new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
                //游戏
            case R.id.fl_2:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet2 bizhiGet2 = retrofit.create(BizhiGet2.class);
                    Call<String> stringCall2 = bizhiGet2.STRING_CALL();
                    stringCall2.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    new Handler().postDelayed(this::Lao, 3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
                //美女
            case R.id.fl_3:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet3 bizhiGet3 = retrofit.create(BizhiGet3.class);
                    Call<String> stringCall3 = bizhiGet3.STRING_CALL();
                    stringCall3.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //风景
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    //  new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
            case R.id.fl_4:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet4 bizhiGet4 = retrofit.create(BizhiGet4.class);
                    Call<String> stringCall4 = bizhiGet4.STRING_CALL();
                    stringCall4.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟关闭
                    //延迟Rec动画
                    Rec();
                    //  new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
                //视觉创意
            case R.id.fl_5:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet5 bizhiGet5 = retrofit.create(BizhiGet5.class);
                    Call<String> stringCall5 = bizhiGet5.STRING_CALL();
                    stringCall5.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    //  new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
                //汽车
            case R.id.fl_6:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet6 bizhiGet6 = retrofit.create(BizhiGet6.class);
                    Call<String> stringCall6 = bizhiGet6.STRING_CALL();
                    stringCall6.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    //  new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
                //萌宠动物
            case R.id.fl_7:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet7 bizhiGet7 = retrofit.create(BizhiGet7.class);
                    Call<String> stringCall7 = bizhiGet7.STRING_CALL();
                    stringCall7.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    //   new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
                //体育
            case R.id.fl_8:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet8 bizhiGet8 = retrofit.create(BizhiGet8.class);
                    Call<String> stringCall8 = bizhiGet8.STRING_CALL();
                    stringCall8.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    // new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
                //清新
            case R.id.fl_9:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet9 bizhiGet9 = retrofit.create(BizhiGet9.class);
                    Call<String> stringCall9 = bizhiGet9.STRING_CALL();
                    stringCall9.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    //  new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
                //卡通动漫
            case R.id.fl_10:
                if (!Utlis.isVPNConnected(this)) {
                    loadDialog.show(false);
                    dataArrayList.clear();
                    BizhiGet10 bizhiGet10 = retrofit.create(BizhiGet10.class);
                    Call<String> stringCall10 = bizhiGet10.STRING_CALL();
                    stringCall10.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if (response.isSuccessful() && response.body() != null) {
                                //加载成功的话关闭弹出
                                Lao();
                                try {
                                    JSONObject jsonObject = new JSONObject(response.body());
                                    String data = jsonObject.getString("data");
                                    JSONArray jsonArray = new JSONArray(data);
                                    StartAdapter(jsonArray);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            //无需处理
                        }
                    });
                    //延迟Rec动画
                    Rec();
                    //延迟关闭
                    //new Handler().postDelayed(this::Lao,3000);
                    item_style.setOnClickListener(this::onClicks);
                    break;
                } else {
                    vpnActivity = new VPNActivity(this);
                }
        }

    }

    public void onClicks(View view) {
        if (item_style_text.getText().equals("0")) {
            recyclerView.setLayoutManager(new GridLayoutManager(ActivityBizhi.this, 1));
            item_style_text.setText("1");
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(bizhi_ll, "Alpha", 0f, 1f);
            objectAnimator.setDuration(2500);
            objectAnimator.start();
            Alerter.create(this)
                    .setDuration(1000)
                    .setTitle("已为你更换！")
                    .setText("可重复点击切换排列方式哦~")
                    .setBackgroundColorRes(R.color.blue_active)
                    .show();
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(ActivityBizhi.this, 2));
            item_style_text.setText("0");
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(bizhi_ll, "Alpha", 0f, 1f);
            objectAnimator.setDuration(2500);
            objectAnimator.start();
            Alerter.create(this)
                    .setDuration(1000)
                    .setTitle("已为你更换！")
                    .setText("可重复点击切换排列方式哦~")
                    .setBackgroundColorRes(R.color.fbutton_color_turquoise)
                    .show();
        }
    }

    private void Rec() {
        objectAnimator = ObjectAnimator.ofFloat(recyclerView, "Alpha", 1f, 0f);
        objectAnimator1 = ObjectAnimator.ofFloat(recyclerView, "Alpha", 0f, 1f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(objectAnimator).with(objectAnimator1);
        animatorSet.setDuration(3000);
        animatorSet.start();
    }
}